package com.karaku.karaku_collector.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.karaku.karaku_collector.LocalSetting;
import com.karaku.karaku_collector.R;
import com.karaku.karaku_collector.common.binder.BindView;
import com.karaku.karaku_collector.common.binder.UIBinder;
import com.karaku.karaku_collector.control.PreviewMaskView;
import com.karaku.karaku_collector.util.LogUtil;

/**
 * Created by guoping on 2017/11/7.
 */

@BindView(R.layout.view_mask_editor)
public class MaskEditorView extends FrameLayout {

    @BindView(R.id.mask_view)
    private PreviewMaskView maskView;
    @BindView(R.id.editor_container)
    private View editorContainer;
    @BindView(R.id.tv_horizontal)
    private TextView tvHorizontal;
    @BindView(R.id.tv_vertical)
    private TextView tvVertical;
    @BindView(R.id.sb_vertical)
    private SeekBar sbVertical;
    @BindView(R.id.sb_horizontal)
    private SeekBar sbHorizontal;
    @BindView(R.id.view_content)
    private View contentView;

    public MaskEditorView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        UIBinder.bind(this, this, true);
        sbHorizontal.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                float v = seekBar.getProgress() * 1.f / seekBar.getMax();
                tvHorizontal.setText(String.format("%.2f", v));
                maskView.setHorizontalRatio(v);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                LogUtil.d("BORDER", "onStopTrackingTouch");
                float v = seekBar.getProgress() * 1.f / seekBar.getMax();
                LocalSetting.getInstance().setCusFrameHorizontalBorderRatio(v);
            }
        });
        sbVertical.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                float v = seekBar.getProgress() * 1.f / seekBar.getMax();
                tvVertical.setText(String.format("%.2f", v));
                maskView.setVerticalRatio(v);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                float v = seekBar.getProgress() * 1.f / seekBar.getMax();
                LocalSetting.getInstance().setCusFrameVerticalBorderRatio(v);
            }
        });
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                hideEditor();
            }
        });
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        maskView.setVerticalRatio(LocalSetting.getInstance().getCusFrameVerticalBorderRatio());
        maskView.setHorizontalRatio(LocalSetting.getInstance().getCusFrameHorizontalBorderRatio());
    }

    public void showEditor() {
        editorContainer.setVisibility(VISIBLE);
        sbHorizontal.setProgress((int)(sbHorizontal.getMax() * LocalSetting.getInstance().getCusFrameHorizontalBorderRatio()));
        sbVertical.setProgress((int)(sbVertical.getMax() * LocalSetting.getInstance().getCusFrameVerticalBorderRatio()));
    }

    public void setContentLayoutParams(LayoutParams params) {
        contentView.setLayoutParams(params);
    }

    public void hideEditor() {
        editorContainer.setVisibility(GONE);
    }

}
